﻿#include "widget.h"
#include "ui_widget.h"


#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>

#include "JlCompressEx.h"


#ifdef Q_OS_WIN
//Q_OS_WIN
// 添加后支持GB2312, utf-8-BOM, Unicode big endian, Unicode little endian
// utf-8无BOM 部分中文编译会失败
// QtCreator建议使用utf-8-BOM编码
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif

#endif




Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    connect(ui->listWidget, &QListWidget::currentRowChanged, [this](int currentRow){
        Q_UNUSED(currentRow)
        if(QListWidgetItem * pItem = ui->listWidget->currentItem())
        {
            QString strText = pItem->text();
            for(QPair<QString,QByteArray>& data : mFilesList)
            {
                if(strText != data.first)
                {
                    continue;
                }
                QFileInfo fileInfo(data.first);
                QString suffix = fileInfo.suffix().toLower();
                if(suffix == "jpg" || suffix == "png")
                {
                    QPixmap pixmap;
                    if(pixmap.loadFromData(data.second, suffix.toStdString().c_str()))
                    {
                        ui->labelImg->setPixmap(pixmap);
                        ui->labelImg->resize(pixmap.width(), pixmap.height());
                        break;
                    }
                    else
                    {
                    }
                }
                else if(suffix == "txt")
                {
                    ui->plainTextEdit->setPlainText(QString::fromUtf8(data.second));
                    break;
                }
            }
        }

    });
}

Widget::~Widget()
{
    delete ui;
}



void Widget::on_pushButtonUnzip_clicked()
{
    QString strZipFile = ui->lineEditZipFile->text();
    QString password = ui->lineEditPassword->text();
    password = password.trimmed();

    QList<QPair<QString,QByteArray>> filesList;
    if(!JlCompressEx::extractToBuffer(strZipFile, password.isEmpty()?NULL:password.toStdString().c_str(), filesList))
    {
        QMessageBox::warning(this, "", tr("打开压缩文件失败"));
        return;
    }
    mFilesList.swap(filesList);

    ui->listWidget->clear();

    for(QPair<QString,QByteArray>& data : mFilesList)
    {
        ui->listWidget->addItem(data.first);

        QFileInfo fileInfo(data.first);
        QString suffix = fileInfo.suffix().toLower();
        if(suffix == "jpg" || suffix == "png")
        {
            QPixmap pixmap;
            if(pixmap.loadFromData(data.second, suffix.toStdString().c_str()))
            {
                ui->labelImg->setPixmap(pixmap);
                ui->labelImg->resize(pixmap.width(), pixmap.height());
                //break;
            }
            else
            {
            }
        }
        else if(suffix == "txt")
        {
            ui->plainTextEdit->setPlainText(QString::fromUtf8(data.second));
        }
    }
}


void Widget::on_pushButtonBrowse_clicked()
{
    QString dirPath = QCoreApplication::applicationDirPath();
    QString strLastFile = ui->lineEditZipFile->text();
    if (!strLastFile.isEmpty() && QFileInfo::exists(strLastFile))
    {
        dirPath = strLastFile;
    }
    else if (!QFileInfo::exists(strLastFile))
    {
        dirPath = "";
    }

    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("打开ZIP文件"),
                                                    dirPath,
                                                    tr("ZIP (*.zip)"));
    if (fileName.isEmpty())
    {
        return;
    }
    qDebug() << fileName;
    ui->lineEditZipFile->setText(fileName);
}


void Widget::on_pushButtonZip_clicked()
{
    QString password = ui->lineEditPassword->text();
    password = password.trimmed();

    //qDebug() << QCoreApplication::applicationDirPath();

    QString strDir = QCoreApplication::applicationDirPath();
    QDir dir(strDir);
    QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot/* | QDir::Dirs*/);
    int iFileCount = 0;
    QStringList fileList;
    for(QFileInfo& fileInfo : fileInfoList)
    {
        if(fileInfo.isDir())
        {// 跳过目录
            continue;
        }
        QString suffix = fileInfo.suffix().toLower();
        if(suffix == "zip" || suffix == "exe")
        {
            continue;
        }
        fileList << fileInfo.filePath();
        if(iFileCount > 10)
        {
            break;
        }
    }
    qDebug() << fileList;
    QBuffer zipBuffer;
    if(!JlCompressEx::CompressToBuffer(fileList, password.isEmpty()?NULL:password.toStdString().c_str(), zipBuffer))
    {
        qDebug() << "压缩失败";
        return;
    }
    const QByteArray &zipData = zipBuffer.data();
    qDebug() << "压缩文件后文件大小：" <<zipData.length();
    QFile fileOut(QCoreApplication::applicationDirPath() + "/2.zip");
    if(!fileOut.open(QIODevice::WriteOnly | QIODevice::Truncate))
    {
        qDebug() << "创建zip文件失败";
        return;
    }
    fileOut.write(zipData);
    fileOut.close();

}

